package inabit.inabitcompiler.productions.constant;

import inabit.compiler.parser.grammar.NonTerminal;
import inabit.compiler.parser.grammar.Production;
import inabit.compiler.parser.grammar.Symbol;
import inabit.compiler.parser.grammar.Terminal;
import inabit.data.Variable;
import inabit.inabitcompiler.lexical.InabitTokenClass;
import inabit.inabitcompiler.parser.InabitSemanticAction;

/**C -> id = G;*/
/**@author Rafael Rocha*/
public class C_Rule extends Production{
	private Terminal id;
	private NonTerminal G;
	
	public C_Rule() {
		super("C");
	}
	
	public Symbol[] production(NonTerminal nonTerminal) {
		id = new Terminal(InabitTokenClass.ID);
		G = new NonTerminal("G");
		
		Symbol[] symbols = {
				id,
				new Terminal(InabitTokenClass.EQUAL),
				G,
				new InabitSemanticAction(nonTerminal) {
					@Override
					public void execute() throws Exception {
						Variable var = getVariable(id.getValue());
						if(var == null) {
							throw new Exception("Variable \'" + id.getValue() + "\' was not declared;");
						}
						var.setValue(G.code);
						var.save();
					}
				},
				new Terminal(InabitTokenClass.SEMI_COLON)
		};
		return symbols;
		
	}
}
